//获取应用实例
const app = getApp();

Page({
    data:{
        now_page: 0,
        list_total: 0,
        data_list: [],
        keywords: '',
        limit: 20,
        nav_list: [],
        active_nav_index: 0,
        tab_value: -1, // 导航值
        
    },
    onLoad: function (options) {
        // 设置标题
        wx.setNavigationBarTitle({
            title: '预生产计划'
        }); 
        // 获取tab列表
        this.get_default_data('onLoad');
    },

    // 每次显示时
    onShow() {
        var __this = this;
        if (!app.check_is_login(false, true)) {
            // 显示登录
            wx.showModal({
                title: '提示',
                content: '请登录后再操作',
                showCancel: false,
                complete() { 
                    __this.get_default_data('onShow-login');
                }
            }); 
        }else{
           
      //   this.setData({
      //     data_list: [],
      //     list_total: 0,
      //     now_page: 0,
      //     keywords: '',
      // });
       // __this.get_default_data('onShow');
        }
      
    },

    // 页面触底时执行
    onReachBottom: function () { 
        this.get_list();
    },

    // 监听下拉[刷新]
    onPullDownRefresh: function () {
        this.setData({
            data_list: [],
            list_total: 0,
            now_page: 0,
            keywords: '',
        });
        // 获取列表
        
        this.get_list();
        // 停止下拉
        setTimeout(() => {
            wx.stopPullDownRefresh();
        }, 1000)
    },

    get_default_data: function (str) {  
        var result = app.request_api('admin/PreProductPlan/getDefaultData', {}, true, 'GET');
        result.then(res => { 
            this.setData({
                nav_list: res.nav_list, 
            }); 
           
            // 获取样板列表 
            this.get_list();
        });
    },

    // 点击菜单
    change_nav: function (events) {
        var index = events.currentTarget.dataset.index;
        this.setData({
            active_nav_index: index,
            data_list: [],
            list_total: 0,
            now_page: 0,
            keywords: '',
        });
        // 重新获取数据 
        this.get_list();
    },

    // 获取列表数据
    get_list: function (is_search = 0) {
      
        var __this = this;
        // 验证
        if ((this.data.data_list.length >= this.data.list_total) && this.data.now_page > 0) {
            return false;
        } 
        // 发起请求
        var result = app.request_api('admin/PreProductPlan/index', {
            page: this.data.now_page + 1,
            quick_search: __this.data.keywords,
            limit: __this.data.limit,
            "params[status]": __this.data.nav_list[__this.data.active_nav_index].value,
        }, true, 'GET');
        result.then(res => { 
            if (is_search == 1) {
              __this.setData({
                    data_list: [],
                    list_total: 0,
                    now_page: 0,
                })
            } 
            __this.setData({
                data_list: __this.data.data_list.concat(res.list),
                list_total: res.total,
                now_page: __this.data.now_page + 1
            });
        });
    },

    // 预览图片
    PreviewImg: function (events) {
        var pic_arr = events.currentTarget.dataset.pic;
        if (pic_arr.length == 0) {
            return false;
        }
        
        // 调用
        wx.previewImage({
            current: pic_arr[0],
            urls: pic_arr
        })
    },

    // 搜索
    GoSearch: function (events) {
        var keywords = events.detail.value.trim();
        this.setData({
            keywords: keywords,
            data_list: [],
            now_page: 0,
            list_total: 0,
        });   
        this.get_list(1);
    },

    // 创建样板
    add_page: function (events) {
        var __this = this;
        wx.showToast({
          title:'调整中...',
          icon: 'none',
          duration: 2000,
      });
      return false;
        // 跳转到创建样板页面
        wx.navigateTo({ 
            url: '/pages/pre_product_plan/add/index',
            events: {
                // 当添加完成时重新请求数据
                changeData: function (data) {
                    __this.setData({
                        keywords: '',
                        data_list: [],
                        now_page: 0,
                        list_total: 0,
                    });  
                    __this.get_list();
                }
            }
        });
    },

    // 编辑样板
    update_page: function (events) {
        var index = events.currentTarget.dataset.index;
        var __this = this;

        // 跳转
        wx.navigateTo({
          url: '/pages/pre_product_plan/add/index?id=' + __this.data.data_list[index].id, 
            events: {
                // 当修改完成时
                changeData: function (data) {

                }
            }
        });
    },

    // 跳转到详情页面
    detail_page: function (events) {
        var index = events.currentTarget.dataset.index;
        var __this = this;
        // 跳转
        wx.navigateTo({  
            url: '/pages/pre_product_plan/detail/index?id=' + __this.data.data_list[index].id,
            events: {
                // 当点击生产按钮时
                changeProductData: function (data) {
                    __this.data.data_list[index].status.value = 3;
                    __this.setData({
                        data_list: __this.data.data_list
                    })
                },
                // 当点击关闭生产按钮时
                changeCloseData: function (data) {
                    __this.data.data_list[index].status.value = 99;
                    __this.setData({
                        data_list: __this.data.data_list
                    })
                }
            }
        });
    },
    



})
